package com.snbc.util;

import net.sf.json.JSONObject;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class SolrUtil implements Watcher{  
    private static Logger logger = LoggerFactory.getLogger(SolrUtil.class);
      
    //缓存时间    
    private String zkHost;
    private static ZooKeeper zooKeeper;    
   
    public SolrUtil(String zkHost){
    	this.zkHost=zkHost;
    }
    
    public ZooKeeper connect() throws IOException, InterruptedException {
    	if(zooKeeper==null){
    		synchronized (this) {
    			zooKeeper = new ZooKeeper(zkHost+"/solr",5000,null);    
			}
    	}
    	return zooKeeper;
     }       
      
     
    public void close() throws InterruptedException {
    	if(zooKeeper!=null){
    		zooKeeper.close();
    	}
    }      
        
    /** 
     * 获取replication列表信息   live_nodes
     * @return 
     * @throws InterruptedException
     * @throws IOException
     * @throws KeeperException 
     */  
    public List<String> getCollectionShards(String collectionName){
    	List<String> list = new ArrayList<String>();
    	try {
			connect();
			byte[] _cluster=zooKeeper.getData("/clusterstate.json",false,null);
	    	JSONObject cluster_obj= JSONObject.fromObject(new String(_cluster));
	    	JSONObject collection_obj=cluster_obj.getJSONObject(collectionName);
	    	if(collection_obj==null || "null".equals(collection_obj.toString())){
	    		return null;
	    	}
	    	JSONObject shards_obj=collection_obj.getJSONObject("shards");
	    	Iterator iter=shards_obj.keys();
	    	while(iter.hasNext()){
	    		list.add((String)iter.next());
	    	}
		} catch (IOException | InterruptedException e) {
			e.printStackTrace();
		} catch (KeeperException e) {
			e.printStackTrace();
		}
    	
       return list;  
    }
    public List<String> printShardsCoreInfo(String collectionName){
    	List<String> list = new ArrayList<String>();
    	try {
			connect();
			byte[] _cluster=zooKeeper.getData("/clusterstate.json",false,null);
	    	JSONObject cluster_obj= JSONObject.fromObject(new String(_cluster));
	    	JSONObject collection_obj=cluster_obj.getJSONObject(collectionName);
	    	JSONObject shards_obj=collection_obj.getJSONObject("shards");
	    	Iterator iter=shards_obj.keys();
	    	String shardName;
	    	System.out.println("shardName====>coreName:");
	    	while(iter.hasNext()){
	    		shardName=(String)iter.next();
	    		JSONObject shardInfo=shards_obj.getJSONObject(shardName);
	    		JSONObject replicasInfo=shardInfo.getJSONObject("replicas");
	    		System.out.println("           "+shardName+"===========>"+getCoreName(replicasInfo));
	    	}
		} catch (IOException | InterruptedException e) {
			e.printStackTrace();
		} catch (KeeperException e) {
			e.printStackTrace();
		}
    	
       return list;  
    }
    private String getCoreName(JSONObject replicasInfo){
    	Iterator iter=replicasInfo.keys();
    	String coreName="";
    	while(iter.hasNext()){
    		JSONObject coreInfo=replicasInfo.getJSONObject((String)iter.next());
    		coreName=coreInfo.getString("core");
    		break;
    	}
    	return coreName;
    }
    public int getSolrLiveNodes() throws IOException, InterruptedException, KeeperException{
    	connect();
    	List<String> list=zooKeeper.getChildren("/live_nodes", false);
    	return list!=null?list.size():0;
    }


	@Override
	public void process(WatchedEvent event) {
		
	}  
	

}  